<?php
/**
 * 专家账户管理
 */
 //返回尽调阶段批次的尽调企业列表（默认只有1个批次处于该阶段）
function get_currentpici_investigate_list(){
	$query = "SELECT investigate_list FROM " .
	           "{pici} where state = ?";
	$queryResult = db_query($query, array(2));
	$investigates = array();
	foreach ($queryResult as $result ) {
	     	$investigates = explode(',',$result->investigate_list);
	}
	return $investigates;
}

//更改尽调阶段批次的尽调企业列表（更改所有处于该阶段的批次）
function set_currentpici_investigate_list($investigate_list){
	db_update('pici')
  		->fields(array(
   			 'investigate_list' => $investigate_list,
  			))
  		->condition('state', 2)
  		->execute();
}

 //返回发布过会企业公示阶段批次的申报企业列表（默认只有1个批次处于该阶段）
function get_finalannounce_pici_fundslist(){
	$query = "SELECT funds_announce_list FROM " .
	           "{pici} where state = ?";
	$queryResult = db_query($query, array(5));
	$funds_announce_list = array();
	foreach ($queryResult as $result ) {
	     	$funds_announce_list = explode(',',$result->funds_announce_list);
	}
	return $funds_announce_list;
}

//更改发布过会企业公示阶段批次的申报企业列表（更改所有处于该阶段的批次）
function set_finalannounce_pici_fundslist($funds_announce_list){
	db_update('pici')
  		->fields(array(
   			 'funds_announce_list' => $funds_announce_list,
  			))
  		->condition('state', 5)
  		->execute();
}

//得到状态为$state的批次id
function get_pici_id_bystate($state){
	$query = "SELECT id FROM " .
	           "{pici} where state = ?";
	$queryResult = db_query($query, array($state));
	$pici_id = 0;
	foreach ($queryResult as $result ) {
	     $pici_id = $result->id;
	}
	return $pici_id;
}

//向前推进批次状态
function set_pici_state($state){
	$newstate = $state+1;
	db_update('pici')
  		->fields(array(
   			 'state' => $newstate,
  			))
  		->condition('state', $state)
  		->execute();
}

function expertmanage_create_page(){
	  //我们为这个页面设置标题
	 drupal_set_title('专家信息管理');
	 global $user;  
	 
	 $query = "SELECT id FROM " .
           "{expert_info} where userid = ?";
 	 $queryResult = db_query($query, array($user->uid));
	 $ifempty =TRUE;
	 $pageid ='';
	  foreach ($queryResult as $links ) {
	  	$pageid =$links->id;
	  	if($pageid > 0)
		  	$ifempty = FALSE;
	  }
	 if($ifempty)
	 {
	 	 $render_array = array(
	   		'#markup' => '',
		 );
	 
		 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
		 $form = drupal_get_form('expertmanage_create_form');
		 $render_array['#markup'] .= drupal_render($form);
	 }
	 else{
	 	drupal_goto('expertmanage/graded');
	 	/* $render_array = array(
	   		'#markup' => '',
		 );
	 
		$form = drupal_get_form('expertmanage_edit_form',$pageid);
  		$render_array['#markup'] .= drupal_render($form);*/
	 }	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function expertmanage_create_form(){	 
	  //提示信息，默认为markup类型。
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['name'] = array(
	  '#title' => t('姓名'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['name'])?$_SESSION['expertmanage_create_form']['name']:"",
	 );
	 
	 $form['corporation'] = array(
	  '#title' => t('单位'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['corporation'])?$_SESSION['expertmanage_create_form']['corporation']:"",
	 );
	 
	 $form['phone'] = array(
	  '#title' => t('座机'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['phone'])?$_SESSION['expertmanage_create_form']['phone']:"",
	 );
	 
	 $form['mobilephone'] = array(
	  '#title' => t('手机'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['mobilephone'])?$_SESSION['expertmanage_create_form']['mobilephone']:"",
	 );
	 
	  $form['mail'] = array(
	  '#title' => t('邮箱地址'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['mail'])?$_SESSION['expertmanage_create_form']['mail']:"",
	 );
	 
	  $form['position'] = array(
	  '#title' => t('职位'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['position'])?$_SESSION['expertmanage_create_form']['position']:"",
	 );
	 
	 $form['research_area'] = array(
	  '#title' => t('专业领域'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['research_area'])?$_SESSION['expertmanage_create_form']['research_area']:"",
	 );
	 	 
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['expertmanage_create_form']['remark'])?$_SESSION['expertmanage_create_form']['remark']:"",
	 );
	 	 
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('确认'),
	 );
	  return $form;
}

function expertmanage_create_form_validate($form, &$form_state){
	 if(!preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $form_state['values']['mail'])){
	  form_set_error('mail',t('您输入的电子邮件地址格式不正确'));
	 }
	  if (drupal_strlen($form_state['values']['name']) < 2) {
	    form_set_error('name', '您输入的姓名长度太短，请填写真实姓名。');
	  }
  
}

function expertmanage_create_form_submit($form, &$form_state){
	  //把表单的值存放在会话中去，由于这里涉及到了两个不同的表单之间传值。
	 $_SESSION['expertmanage_create_form'] = $form_state['values'];
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'expertmanage/create/confirm';
}

function expertmanage_confirm_page(){
  //我们为这个页面设置标题
	 drupal_set_title('提交专家信息预览');
	 
	 //这里首先作了判断，如果会话中没有设置contactus_form，返回contactus
	 if(empty($_SESSION['expertmanage_create_form'])){
	  drupal_goto('expertmanage/create');
	 }
	 $render_array = array(
	   '#markup' => '', 
	 );
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('expertmanage_confirm_form');
	 $render_array['#markup'] .= drupal_render($form);
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function expertmanage_confirm_form(){
 $form['name'] = array(
  '#title' => t('姓名'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['name'])?$_SESSION['expertmanage_create_form']['name']:"",
 );
 
 $form['corporation'] = array(
  '#title' => t('单位'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['corporation'])?$_SESSION['expertmanage_create_form']['corporation']:"",
 );
 
 $form['phone'] = array(
  '#title' => t('座机'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['phone'])?$_SESSION['expertmanage_create_form']['phone']:"",
 );
 
 $form['mobilephone'] = array(
  '#title' => t('手机'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['mobilephone'])?$_SESSION['expertmanage_create_form']['mobilephone']:"",
 );
 
 $form['mail'] = array(
  '#title' => t('邮箱地址'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['mail'])?$_SESSION['expertmanage_create_form']['mail']:"",
 );
 
 $form['position'] = array(
  '#title' => t('职位'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['position'])?$_SESSION['expertmanage_create_form']['position']:"",
 );
 
 $form['research_area'] = array(
  '#title' => t('专业领域'),
  '#type' => 'item',
  '#markup' => isset($_SESSION['expertmanage_create_form']['research_area'])?$_SESSION['expertmanage_create_form']['research_area']:"",
 );
  
 $form['remark'] = array(
  '#title' => t('备注'),
  '#type' => 'item',
   '#markup' =>isset($_SESSION['expertmanage_create_form']['remark'])?$_SESSION['expertmanage_create_form']['remark']:"",
 );
	 
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('返回'),
    '#submit' => array('expertmanage_confirm_form_back'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('提交'),
  );
  
  return $form;
}

function expertmanage_confirm_form_back($form, &$form_state){
  //简单的重定向到contactus页面
 $form_state['redirect'] = 'expertmanage/create';
}

function expertmanage_confirm_form_submit($form, &$form_state){
		/**
	   * 写入数据库
	   */
	   $values = NULL;
	   //从会话中获取用户最初提交的值，并将$_SESSION['contactus_form']置为空。
	  if(empty($_SESSION['expertmanage_create_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'expertmanage/create';
	  }else{
		  $values = $_SESSION['expertmanage_create_form'];
		  unset($_SESSION['expertmanage_create_form']);
	  }
	  global $user;
	  $last_id = db_insert('expert_info')
  		->fields(array(
  		     'userid'=> $user->uid,
   			 'corporation' => $values['corporation'],
   			 'name' => $values['name'],
   			 'phone' => $values['phone'],
   			 'mobilephone' => $values['mobilephone'],
   			 'mail' => $values['mail'],
   			 'position' =>  $values['position'],
   			 'research_area' =>  $values['research_area'],
   			 'remark' => $values['remark'],
  			))->execute();
	  if ($last_id!=NULL) {
	    /**
	     * 执行成功了，到数据库去看看，是不是有这条记录了
	     * 显示一条信息给用户
	     */
		 $form_state['redirect'] = 'expertmanage/graded';  
	  } else {
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }
		
	
}

function expertmanage_list_page() {
 $header = array(
      array('data' => '姓名', 'field' => 'name', 'sort' => 'desc'),
      array('data' => '职位', 'field' => 'position'),
      array('data' => '电话', 'field' => 'phone'),
      array('data' => '邮箱', 'field' => 'mail'),
      array('data' => '单位', 'field' => 'corporation'),
      array('data' => '操作', 'field' => 'option'),
      );
  $query = db_select('expert_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'position',
      'phone',
      'mail',
      'corporation',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
      $rows[] = array(
      'data' => array(
        l($node->name, 'expertmanage/edit/'. $node->id),
        $node->position,
        $node->phone,
        $node->mail,
        $node->corporation,
        l('编辑', 'expertmanage/edit/'. $node->id),
      )
      );
  }
  $html = theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有专家信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}

function expertmanage_list_perpage($perid) {
  // content variable that will be returned for display
  $page_content = '';
  $page_content .= l('查看专家信息列表','expertmanage/list') . '<br />'. '<br />';
  
  $query = "SELECT name,corporation,phone,mobilephone,mail,position,research_area,remark FROM " .
           "{expert_info} where id = ?";
  $queryResult = db_query($query, array($perid));
  foreach ($queryResult as $links ) { 
    $page_content .= '<h5>姓名：'.$links->name.'</h5><br />';
	$page_content .= '<h5>单位：'.$links->corporation.'</h5><br />';
	$page_content .= '<h5>座机：'.$links->phone.'</h5><br />';
	$page_content .= '<h5>手机：'.$links->mobilephone.'</h5><br />';
	$page_content .= '<h5>邮箱地址：'.$links->mail.'</h5><br />';
	$page_content .= '<h5>职位'.$links->position.'</h5><br />';
    $page_content .= '<h5>专业领域：'.$links->research_area.'</h5><br />';
    $page_content .= '<h5>备注：'.$links->remark.'</h5><br />';
	$page_content = str_replace(array("\n", "\r\n") , "<BR />", $page_content);
  }
  // More coming....
  if ($page_content == '') {
    $page_content = "没有该专家信息.";
  }

  return $page_content;
}

function expertmanage_edit_page($perid) {
  $page_content = '';
  if(user_access('manage experts information'))
  	$page_content .= l('查看专家信息列表','expertmanage/list') . '<br />'. '<br />';
  drupal_set_title('专家信息管理');
	 
	 $query = "SELECT id FROM " .
           "{expert_info} where id = ?";
 	 $queryResult = db_query($query, array($perid));
	 $ifempty =TRUE;
	  foreach ($queryResult as $links ) {
	  	if($links->id>0)
		  	$ifempty = FALSE;
	  }
	 if($ifempty)
	 {
	 	 //$render_array = array(
	   		//'#markup' => '',
		 //);
	     drupal_goto('expertmanage/list');
		 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
		 //$form = drupal_get_form('expertmanage_create_form');
		 //$render_array['#markup'] .= drupal_render($form);
	 }
	 else{
	 	 $render_array = array(
	   		'#markup' => $page_content,
		 );
	 
		$form = drupal_get_form('expertmanage_edit_form',$perid);
  		$render_array['#markup'] .= drupal_render($form);
	 }	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function expertmanage_edit_form($form, &$form_state, $perid){
  
  $query = "SELECT name,corporation,phone,mobilephone,mail,position,research_area,remark FROM " .
           "{expert_info} where id = ?";
  $queryResult = db_query($query, array($perid));
 
  $name = '';
  $corporation = '';
  $phone = '';
  $mobilephone = '';
  $mail = ''; 
  $position = '';
  $research_area = '';
  $remark = '';
  
  foreach ($queryResult as $links ) {
  	$name = $links->name;
    $corporation = $links->corporation;
	$phone = $links->phone;
	$mobilephone = $links->mobilephone;
    $mail = $links->mail;
	$position = $links->position;
	$research_area = $links->research_area;
    $remark = $links->remark;
  }
   $form['bid'] = array('#type' => 'hidden', '#value' => $perid);
   $form['name'] = array(
	  '#title' => t('姓名'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['name'])?$_SESSION['expertmanage_edit_form']['name']:$name,
	 );
	 
	 $form['corporation'] = array(
	  '#title' => t('单位'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['corporation'])?$_SESSION['expertmanage_edit_form']['corporation']:$corporation,
	 );
	 
      $form['phone'] = array(
	  '#title' => t('座机'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['phone'])?$_SESSION['expertmanage_edit_form']['phone']:$phone,
	 );
	 
	 $form['mobilephone'] = array(
	  '#title' => t('手机'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['mobilephone'])?$_SESSION['expertmanage_edit_form']['mobilephone']:$mobilephone,
	 );
  	 
	 $form['mail'] = array(
	  '#title' => t('邮箱地址'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['mail'])?$_SESSION['expertmanage_edit_form']['mail']:$mail,
	 );
	 
	 $form['position'] = array(
	  '#title' => t('职位'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['position'])?$_SESSION['expertmanage_edit_form']['position']:$position,
	 );
	 
	 $form['research_area'] = array(
	  '#title' => t('专业领域'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['research_area'])?$_SESSION['expertmanage_edit_form']['research_area']:$research_area,
	 );
	 
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['expertmanage_edit_form']['remark'])?$_SESSION['expertmanage_edit_form']['remark']:$remark,
	 );
	
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交修改'),
	  );
	 //表单元素“提交”按钮
	  $form['deleate'] = array(
	    '#type' => 'submit',
	    '#value' => t('删除专家信息'),
	    '#submit' => array('expertmanage_edit_form_deleate'),
	  );
  return $form;
}

function expertmanage_edit_form_submit($form, &$form_state){
	  $values = NULL;
	  $values = $form_state['values'];		
	  db_update('expert_info')
  		->fields(array(
   			 'corporation' => $values['corporation'],
   			 'name' => $values['name'],
   			 'phone' => $values['phone'],
   			 'mobilephone' => $values['mobilephone'],
   			 'mail' => $values['mail'],
   			 'position' =>  $values['position'],
   			 'research_area' =>  $values['research_area'],
   			 'remark' => $values['remark'],
  			))
  		->condition('id', $values['bid'])
  		->execute();
	 $form_state['redirect'] = 'expertmanage/edit/'.$values['bid'];
}

function expertmanage_edit_form_deleate($form, &$form_state){
	 $values = NULL;
	 $_SESSION['expertmanage_edit_form'] = $form_state['values'];
	 $values = $_SESSION['expertmanage_edit_form'] ;
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'expertmanage/deleate/'.$values['bid'];
}

function expertmanage_deleate_page($perid) {
  // content variable that will be returned for display
  $page_content = '删除后专家信息将不可恢复！';
  
  drupal_set_title('删除专家信息');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('expertmanage_deleate_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function expertmanage_deleate_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('expertmanage_deleate_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
  );
  
  return $form;
}

function expertmanage_deleate_form_back($form, &$form_state){
	if(empty($_SESSION['expertmanage_edit_form'])){
		  if(user_access('view experts information'))
		  	$form_state['redirect'] = 'expertmanage/list';
		  else{
		  	$form_state['redirect'] = 'expertmanage/create';
		  }
	  }else{
		  $values = $_SESSION['expertmanage_edit_form'];
		  unset($_SESSION['expertmanage_edit_form']);
		  $form_state['redirect'] = 'expertmanage/edit/'.$values['bid'];
	  }
}

function expertmanage_deleate_form_submit($form, &$form_state){
  //简单的重定向到contactus页面
 if(empty($_SESSION['expertmanage_edit_form'])){
		  //drupal_goto('contact');
		   if(user_access('view experts information'))
		  	$form_state['redirect'] = 'expertmanage/list';
		  else{
		  	$form_state['redirect'] = 'expertmanage/create';
		  }
	  }else{
		  $values = $_SESSION['expertmanage_edit_form'];
		  unset($_SESSION['expertmanage_edit_form']);
		  db_delete('expert_info')
			->condition('id' ,$values['bid'])
			->execute();
		  if(user_access('view experts information'))
		  	$form_state['redirect'] = 'expertmanage/list';
		  else
		  	$form_state['redirect'] = 'expertmanage/create';
	  }

}

function expertmanage_choose_page() {
  drupal_set_title('专家评审管理');
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
  $page_content = '';
  //$applyfunds_info = array();
  
  $applyfunds_info = get_part_by_state(3);
  //插入待申报基金列表
  
  $page_content .= "<div><table style='width: 800px'><tr><th colspan=7>待评审基金列表</th></tr>".
  "<tr><td style='width: 10%'>编号</td>".
  "<td style='width: 15%'>拟设基金名称</td>".
  "<td style='width: 15%'>规模</td>".
  "<td style='width: 15%'>主投领域</td>".
  "<td style='width: 15%'>管理机构</td>".
  "<td style='width: 15%'>联系人</td>".
  "<td style='width: 15%'>联系电话</td></tr>";
  $count = 0;
  foreach ($applyfunds_info as $fund_info ) {
  	$count += 1;
  	$page_content .= ' <tr><td >'.$count.'</td>'.
  	'<td >'.$fund_info["nishejijinmingcheng"].'</td>'.
  	'<td >'.$fund_info["jijinguimo"].'</td>'.
  	'<td >'.$fund_info["zhuyaotouzilingyu"].'</td>'.
  	'<td >'.$fund_info["guanlijigoumingcheng"].'</td>'.
  	'<td >'.$fund_info["lianxiren"].'</td>'.
  	'<td >'.$fund_info["lianxidianhua"].'</td></tr>';
  }
  $page_content .= "</table></div>"; 
  
  if($count == 0){
  	$page_content = '当前没有待评审申报基金。';
  	$render_array = array(
  		'#markup' => $page_content,
  	);
  }
  else{
    
	 $query = "SELECT id FROM " .
           "{pici} where state = ?";
     $queryResult = db_query($query, array(0));
	 $count = 0;
	 $form = null;
     foreach ($queryResult as $result ) {
     	$count +=1;
     	$form = drupal_get_form('expertmanage_choose_perform',$result->id);
     }
	 if($count == 0) {
	 	 $query = "SELECT id,pingshen_remark, experts_list FROM " .
           "{pici} where state = ?";
	     $piciResult = db_query($query, array(1));
		 $count = 0;
	     foreach ($piciResult as $result ) {
	     	$count +=1;
	     	$page_content .= "<table style='width: 800px'><tr><th colspan=1>评审说明</th></tr><br><br>".
  				"<tr><td >".$result->pingshen_remark."</td></tr></table><br><br>";
			
			$query = "SELECT experts_list,  pingshen_remark FROM " .
           		"{pici} where id = ?";
		    $expertsResult = db_query($query, array($result->id));
		
		    $experts = array();
		    foreach ($expertsResult as $result) {
		       $experts = explode(',',$result->experts_list);
		    }
			$page_content .= "<table style='width: 800px'><tr><th colspan=6>所选专家列表</th></tr>";
  			$page_content .= "<tr><td >编号</td><td >姓名</td><td >单位</td><td >职位</td><td >手机</td><td >座机</td></tr>";	
			$count = 0;
			//foreach ($experts as $expert) {
				$count+=1;
				$query = "SELECT id, name, position, phone, mobilephone, corporation FROM " .
		           "{expert_info} ";
		    	$expertInfos = db_query($query);
				foreach ($expertInfos as $expertinfo) {
					if(in_array($expertinfo->id,$experts, TRUE)&&$experts!=NULL){
					$page_content .= "<tr><td >".$count."</td><td >".$expertinfo->name."</td><td >".$expertinfo->corporation
					."</td><td >".$expertinfo->position."</td><td >".$expertinfo->mobilephone."</td><td >".$expertinfo->phone."</td></tr>";	
			
				}	
				}
			//}
		   	$page_content .= "</table>";  
	     }
	     if($count==0)
			 $form = drupal_get_form('expertmanage_choose_form');
	 }
	 $render_array = array(
  	 	'#markup' => $page_content,
     );
  	 $render_array['#markup'] .= drupal_render($form ); 
  }
  return $render_array; 
}

function expertmanage_choose_perform($form, &$form_state, $pici_id){
  $query = "SELECT experts_list,  pingshen_remark FROM " .
           "{pici} where id = ?";
  $queryResult = db_query($query, array($pici_id));

  $experts = array();
  //$funds = array();
  $pingshen_remark = '';
  foreach ($queryResult as $result) {
      $experts = explode(',',$result->experts_list);
	  //$funds = explode(',',$result->funds_list);
	  $pingshen_remark = $result->pingshen_remark;
  }
  $query = "SELECT id, name, position, phone,mobilephone, corporation FROM " .
           "{expert_info}";
  $queryResult = db_query($query);	
  
  $header = array(
    'name' => t('姓名'),
    'corporation' => t('单位'),
    'position' => t('职位'),
    'mobilephone' => t('手机'),
    'phone' => t('座机'),
    //'state' => t('状态(空表示未被选择)'),
  );
  $form['bid'] = array('#type' => 'hidden', '#value' => $pici_id);
  
  $form['pingshen_remark'] = array(
	  '#title' => t('评审说明'),
	  '#type' => 'textarea',
	  '#default_value' =>isset($_SESSION['expertmanage_choose_perform']['pingshen_remark'])?$_SESSION['expertmanage_choose_perform']['pingshen_remark']:$pingshen_remark,
	 );
	 
  $options = array();
  foreach ($queryResult as $node) {
  	//$state = '';
  	//if(in_array($node->id,$experts, TRUE)&&$experts!=NULL){
  		//$state=  '已选择';
  	//}
    $options[$node->id] =array(
      'name' => l($node->name, 'expertmanage/list/'. $node->id),
      'corporation' => $node->corporation,  
      'position' => $node->position,
      'mobilephone' => $node->mobilephone,
      'phone' => $node->phone,
      //'state' => $state,   
    );
  }

  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('没有专家记录'),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('暂存'),
  );
  
  $form['final_submit'] = array(
    '#type' => 'submit',
    '#value' => t('最终提交（将不可再修改，只有当所有评审专家都填写完意见后才可在此提交！！）'),
    '#submit' => array('expertmanage_choose_perform_final_submit'),
  );
  
  return $form;  
}

function expertmanage_choose_form($form, &$form_state){
  $experts = array();
  $query = "SELECT id, name, position, phone, mobilephone, corporation FROM " .
           "{expert_info}";
  $queryResult = db_query($query);	
  
  $header = array(
    'name' => t('姓名'),
    'corporation' => t('单位'),
    'position' => t('职位'),
    'mobilephone' => t('手机'),
    'phone' => t('座机'),
    //'state' => t('状态(空表示未被选择)'),
  );  
  $form['pingshen_remark'] = array(
	  '#title' => t('评审说明'),
	  '#type' => 'textarea',
	  '#default_value' =>isset($_SESSION['expertmanage_choose_perform']['pingshen_remark'])?$_SESSION['expertmanage_choose_perform']['pingshen_remark']:"",
	 );
	 
  $options = array();
  foreach ($queryResult as $node) {
  	//$state = '';
  	//if(in_array($node->id,$experts, TRUE)&&$experts!=NULL){
  		//$state=  '已选择';
  	//}
    $options[$node->id] =array(
      'name' => l($node->name, 'expertmanage/list/'. $node->id),
      'corporation' => $node->corporation,  
      'position' => $node->position,
      'mobilephone' => $node->mobilephone,
      'phone' => $node->phone,
      //'state' => $state,   
    );
  }

  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('没有专家记录'),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('暂存'),
  );
  
  $form['final_submit'] = array(
    '#type' => 'submit',
    '#value' => t('最终提交（将不可再修改）'),
    '#submit' => array('expertmanage_choose_form_final_submit'),
  );
  return $form;  
}

function expertmanage_choose_form_submit($form, &$form_state){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	 $values = $form_state['values'];		
	 $experts_list = '';
	 $funds_list = '';
	 $pingshen_remark = $values['pingshen_remark'];
	 foreach ($values['table'] as $rows) {
         $experts_list.=$rows.',';
     }
	 $funds_array = get_part_by_state(3);
	 foreach ($funds_array as $fund_id) {
         $funds_list.=$fund_id['id'].',';
     }
	  $last_id = db_insert('pici')
  		->fields(array(
  		     'experts_list'=> $experts_list,
   			 'pingshen_remark' => $pingshen_remark,
   			 'funds_list' => $funds_list,
   			 'state' => 0,
  			))->execute();
	  if ($last_id!=NULL) {
	    /**
	     * 执行成功了，到数据库去看看，是不是有这条记录了
	     * 显示一条信息给用户
	     */
		 $form_state['redirect'] = 'expertmanage/choose';  
	  } else {
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	  }	
}

function expertmanage_choose_form_final_submit($form, &$form_state){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	 $values = $form_state['values'];		
	 $experts_list = '';
	 $funds_list = '';
	 $funds_array = get_part_by_state(3);
	 foreach ($funds_array as $fund_id) {
         $funds_list.=$fund_id['id'].',';
     }
	 $pingshen_remark = $values['pingshen_remark'];
	 foreach ($values['table'] as $rows) {
         $experts_list.=$rows.',';
     }

     //先把原来的评审专家评审记录删除
     foreach ($funds_array as $fund_id) {
         $delete_old_record = db_delete('expert_verdict')
             ->condition('apply_id' , $fund_id['id'])
             ->execute();
     }

	  $last_id = db_insert('pici')
  		->fields(array(
  		     'experts_list'=> $experts_list,
   			 'pingshen_remark' => $pingshen_remark,
   			 'funds_list' => $funds_list,
   			 'state' => 1,
  			))->execute();
	  if ($last_id!=NULL) {
	    /**
	     * 执行成功了，到数据库去看看，是不是有这条记录了
	     * 显示一条信息给用户
	     */
		 $form_state['redirect'] = 'expertmanage/choose';  
	  } else {
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }	
}

function expertmanage_choose_perform_submit($form, &$form_state){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	 $values = $form_state['values'];		
	 $experts_list = '';
	 
	 $funds_list = '';
	 $funds_array = get_part_by_state(3);
	 foreach ($funds_array as $fund_id) {
         $funds_list.=$fund_id['id'].',';
     }
	 $pingshen_remark = $values['pingshen_remark'];
	 foreach ($values['table'] as $rows) {
         $experts_list.=$rows.',';
     }
	  db_update('pici')
  		->fields(array(
   			 'experts_list' => $experts_list,
   			 'pingshen_remark' => $pingshen_remark,
   			 'funds_list' => $funds_list,
   			 'state' => 0,
  			))
  		->condition('id', $values['bid'])
  		->execute();
	 $form_state['redirect'] = 'expertmanage/choose';	
}

function expertmanage_choose_perform_final_submit($form, &$form_state){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	 $values = NULL;
	 $values = $form_state['values'];		
	 $experts_list = '';
	  
	 $funds_list = '';
	 $funds_array = get_part_by_state(3);
	 foreach ($funds_array as $fund_id) {
         $funds_list.=$fund_id['id'].',';
     }
	 $pingshen_remark = $values['pingshen_remark'];
	 foreach ($values['table'] as $rows) {
         $experts_list.=$rows.',';
     }
	  db_update('pici')
  		->fields(array(
   			 'experts_list' => $experts_list,
   			 'pingshen_remark' => $pingshen_remark,
   			 'funds_list' => $funds_list,
   			 'state' => 1,
  			))
  		->condition('id', $values['bid'])
  		->execute();
	 $form_state['redirect'] = 'expertmanage/choose';	
}

///////
//专家评分模块
///////

//专家账户页面
function expertmanage_graded_choosepage() {
 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
 $html = ''; 
 global $user;
 
 $query = "SELECT id FROM " .
           "{expert_info} where userid = ?";
 $experts_list = db_query($query, array($user->uid));	
 $current_expertid = '';
 foreach ($experts_list as $expert) {
      $current_expertid = $expert->id;
 }		   
 if($current_expertid > 0){
 	  global $user;
	  $page_content = '';
	  $render_array = null;
	  $query = "SELECT experts_list, pingshen_remark FROM " .
	           "{pici} where state = ?";
	  $queryResult = db_query($query, array(1));
	  $count = 0;
	  $experts = array();
	  $pingshen_remark = '';
	  foreach ($queryResult as $result ) {
	     	$count +=1;
	     	$experts = explode(',',$result->experts_list);
			$pingshen_remark = $result->pingshen_remark;
	  }
	  if(($count>0)&&in_array($current_expertid, $experts, TRUE)){
	  	  
	  	  $page_content .= "<table style='width: 850px'><tr > <td style='width: 20%'>评审说明：</td><td style='width: 80%'>".
	  	  $pingshen_remark."</td></tr></table><br>";
          //////////
		  //插入待评审基金列表
		  /////////
          //$applyfunds_info = array();
          $applyfunds_info = get_part_by_state(3);
		  $page_content .= "<div><table style='width: 850px'><tr><th colspan=5>待评审基金列表</th></tr>".
			  "<tr><td style='width: 10%'>编号</td>".
			  "<td style='width: 40%'>拟设基金名称</td>".
			  "<td style='width: 15%'>评审意见</td>".
			  "<td style='width: 15%'>综合打分</td>".
			  "<td style='width: 20%'>操作</td></tr>";
			  $count = 0;
			  $query = "SELECT id  FROM " .
				           "{expert_info} where userid = ? ";
			  $queryResult = db_query($query, array($user->uid));
			  $expert_id = '';  //
			  foreach ($queryResult as $links ) {
				if($links->id>0){
					$expert_id = $links->id;
				}
			 }
			  foreach ($applyfunds_info as $fund_info ) {
			  		
			  		$query = "SELECT id ,state, opinion,score FROM " .
           				"{expert_verdict} where expert_info_id = ? and apply_id =?";
					$if_verdict = '';
					$opinion = '';
					$score = '';
	  				$expertsResult = db_query($query, array($expert_id, $fund_info['id'] ));
					foreach ($expertsResult as $expertResult ) {
						if($expertResult->state==1)
						{
							$html .= '<h4><center>您当前没有需要审批的基金。</center></h4>';
 							return ($html); 
						}
						$if_verdict = '(已评审)';						
						if($expertResult->opinion==0)
							$opinion = '推荐';
						else if($expertResult->opinion==1)
							$opinion = '有条件推荐';
						else if($expertResult->opinion==2)
							$opinion = '不推荐';
						$score = $expertResult->score;
					}
			  	
			  	$count += 1;
			  	$page_content .= ' <tr><td >'.$count.$if_verdict.'</td>'.
			  	'<td >'.$fund_info['nishejijinmingcheng'].'</td>'.
			  	'<td >'.$opinion.'</td>'.
			  	'<td >'.$score.'</td>';
			  	if($if_verdict == '(已评审)')
			  		$page_content .='<td >'.l('修改','expertmanage/graded_apply/'.$fund_info['id']).'</td></tr>';
				else
					$page_content .='<td >'.l('进入评审','expertmanage/graded_apply/'.$fund_info['id']).'</td></tr>';
			  }
			  $page_content .= "</table></div><br><br>"; 
         
		  $render_array = array(
		  	'#markup' => $page_content,
		  );
		  	 
		  $form = drupal_get_form('expertmanage_graded_choose_form');
		  $render_array['#markup'] .= drupal_render($form ); 
	  }
	  else{
	  	  $page_content .= '<h4><center>您当前没有需要审批的基金。</center></h4>';
	   	  $render_array = array(
		  	'#markup' => $page_content,
		  );
	  }
	  return $render_array; 
 }		   
 else{
  $html .= l('请填写专家信息！','expertmanage/create');
 }
  return ($html); 
}

function expertmanage_graded_choose_form($form, &$form_state){
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交评审结果（提交后不可再修改）'),
	  );	
	 return $form;
}

function expertmanage_graded_choose_confirm_form_submit($form, &$form_state){
	 global $user;
	  $query = "SELECT id  FROM " .
           "{expert_info} where userid = ? ";
	$queryResult = db_query($query, array($user->uid));
    $expert_id = '';  //
	foreach ($queryResult as $links ) {
		if($links->id>0){
			$expert_id = $links->id;
		}
	}
	  db_update('expert_verdict')
  		->fields(array(
   			 'state' => 1,
  			))
  		->condition('state', 0)
		->condition('expert_info_id',$expert_id)
  		->execute();
	 $form_state['redirect'] = 'expertmanage/graded';	
}
function expertmanage_graded_choose_confirmpage() {
  // content variable that will be returned for display
  $page_content = '请确认填写内容是否是您真实意见表示，有无笔误，提交后将无法再修改！';
  
  drupal_set_title('提交评审结果');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('expertmanage_graded_choose_confirm_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function expertmanage_graded_choose_confirm_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('expertmanage_graded_choose_confirm_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('提交'),
  );
  
  return $form;
}

function expertmanage_graded_choose_confirm_form_back($form, &$form_state){
  $form_state['redirect'] = 'expertmanage/graded';
}

function expertmanage_graded_choose_form_submit($form, &$form_state){
	 $form_state['redirect'] = 'expertmanage/graded_confirm';
}

function expertmanage_graded_applypage($applyid) {
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
  $page_content = '';
  drupal_set_title('申报材料专家评分');   
  global $user;
  
  ///判断是否在审批阶段
  
  $page_content .= l('返回列表','expertmanage/graded') . '<br />'. '<br />';	
  $page_content .= enterprise_info_id($applyid);
  $page_content .="<br><br>";
	 //$page_content .= l('切换成浏览模式','expertmanage/graded_apply_view/'.$applyid) . '<br />'. '<br />'; 
  $page_content .= "<div><table style='width: 650px'>".
			  "<tr><th style='width: 30%'>评价指标</td><th style='width: 70%'>建议参考子项</td></tr>".
			  "<tr><td rowspan='4' >一、管理机构（50分）</td><td>管理机构实力及信誉</td></tr><tr><td>管理团队能力及业绩</td></tr>".
			  "<tr><td>管理机构运作规范程度</td></tr><tr><td>管理机构增值服务能力</td></tr>".
			  "<tr><td>二、发起人（20分）</td><td>主发起人和其他投资人实力及信誉</td></tr>".
			  "<tr><td rowspan='2'>三、基金运营方案（30分）</td><td>项目遴选、投资决策、风险控制、分配与激励机制</td></tr><tr><td>储备项目、投资计划及在青比重</td></tr>".
			  "</table></div>";
  $render_array = array(
	  '#markup' => $page_content,
  );
	 
  $form = drupal_get_form('expertmanage_graded_editform',$applyid);
  
  $render_array['#markup'] .= drupal_render($form);
  return $render_array;    
}

function expertmanage_graded_editform($form, &$form_state, $applyid){
    global $user;
	$query = "SELECT id  FROM " .
           "{expert_info} where userid = ? ";
	$queryResult = db_query($query, array($user->uid));
    $expert_id = '';  //
	foreach ($queryResult as $links ) {
		if($links->id>0){
			$expert_id = $links->id;
		}
	}
		
  $query = "SELECT id ,assess,opinion,score,remark,state FROM " .
           "{expert_verdict} where expert_info_id = ? and apply_id =?";

  $queryResult = db_query($query, array($expert_id, $applyid ));
	$assess = '';
	$opinion = '';
	$score = '';
	$state = '';
	$remark = '';
	foreach ($queryResult as $links ) {
		if($links->id>0){
			$assess = $links->assess;
			$opinion = $links->opinion;
			$score = $links->score;
			$remark = $links->remark;
			$state = $links->state;
		}
	}
	$options = array('0' => t('推荐'), '1' => t('有条件推荐'),'2' => t('不推荐'));
	if($state==1)
		drupal_goto('expertmanage/graded');
	
   	$form['bid'] = array('#type' => 'hidden', '#value' => $applyid);
	
	$form['score'] = array(
	  '#title' => t('综合打分'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_graded_editform']['score'])?$_SESSION['expertmanage_graded_editform']['score']:$score,
	 );
    
    $form['assess'] = array(
	  '#title' => t('综合意见'),
	  '#type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_graded_editform']['assess'])?$_SESSION['expertmanage_graded_editform']['assess']:$assess,
	 );
	
	$form['opinion'] = array(
	  '#title' => t('推荐意见'),
	  '#type' => 'select',
	  '#required' => TRUE,
	  '#options' => $options,
	  '#default_value' =>isset($_SESSION['expertmanage_graded_editform']['opinion'])?$_SESSION['expertmanage_graded_editform']['opinion']:$opinion,
	
	  //'#default_value' =>isset(variable_get('opinion',$_SESSION['expertmanage_graded_editform']['opinion']))?
	  	//	variable_get('opinion',$_SESSION['expertmanage_graded_editform']['opinion']):variable_get('opinion',$assess),
	 );
	
    $form['remark'] = array(
	  '#title' => t('推荐理由'),
	  '#type' => 'textarea',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['expertmanage_graded_editform']['remark'])?$_SESSION['expertmanage_graded_editform']['remark']:$remark,
	);
	 	 
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('保存'),
	  );	
	
  return $form;
}

function expertmanage_graded_editform_validate($form, &$form_state){
	  if (drupal_strlen($form_state['values']['score']) > 10) {
	    form_set_error('score', '要求精度小于10 个字符');
	  } 
	  if (!($form_state['values']['score']>0)) {
	    form_set_error('score', '请输入正数');
	  } 
	  
}

function expertmanage_graded_editform_submit($form, &$form_state){
	$values=$form_state['values'];	
	global $user; 
	$query = "SELECT id  FROM " .
           "{expert_info} where userid = ? ";
	$queryResult = db_query($query, array($user->uid));
    $expert_id = '';  //
	foreach ($queryResult as $links ) {
		if($links->id>0){
			$expert_id = $links->id;
		}
	}	 
    $query = "SELECT id  FROM " .
           "{expert_verdict} where expert_info_id = ? and apply_id =?";
    $queryResult = db_query($query, array($expert_id, $values['bid'] ));
    $verdictid = '';  //
	foreach ($queryResult as $links ) {
		if($links->id>0){
			$verdictid = $links->id;
		}
	}
		if($verdictid>0){
			db_update('expert_verdict')
			  	->fields(array(
			   		 'assess'=>$values['assess'],
		   			 'opinion' => $values['opinion'],
		   			 'score' => $values['score'],
		   			 'remark'=>$values['remark'],
		   			 'state' => 0,
			  	))
			  	->condition('id',$verdictid)
			  	->execute();	
		}		
		else {
			$last_id = db_insert('expert_verdict')
		  		->fields(array(
		   			 'expert_info_id' => $expert_id,
		   			 'apply_id' => $values['bid'],
		   			 'assess'=>$values['assess'],
		   			 'opinion' => $values['opinion'],
		   			 'score' => $values['score'],
		   			 'remark'=>$values['remark'],
		   			 'state' => 0,
		  			))->execute();			
		}
	$form_state['redirect'] = 'expertmanage/graded';
}
/*
//专家浏览打分结果
//传人专家给分结果id
function expertmanage_graded_apply_viewpage($perid) {
    $page_content = '';
    global $user; 
    $query = "SELECT  id ,assess,opinion,score,remark,state  FROM " .
           "{expert_verdict} where id = ? ";
    global $user;
    $queryResult = db_query($query, array( $perid));
    $verdictid = '';
	$verdict = '';
	foreach ($queryResult as $verdict ) {
		if($verdict->id>0){
			$verdictid = $verdict->id;
		}
	}
	if($verdictid>0){
		$page_content .= l('查看列表','expertmanage/all_graded_fund_view/') . '<br />'. '<br />';	
    }
  else {
    drupal_goto('expertmanage/all_graded_fund_view');
  }

  return $page_content;
}
*/

/*
//对应申报材料所有专家评分结果列表
//传人评审材料id
function expertmanage_allgraded_apply_viewpage($apply_id) {
    	
  $html = l('返回基金列表','expertmanage/graded_result/fundlist') . '<br />'. '<br />'; 
  $header = array(
      array('data' => '专家名称'),
      array('data' => '管理机构得分', 'field' => 'score_department'),
      array('data' => '发起人得分', 'field' => 'score_applyman'),
      array('data' => '运营方案得分', 'field' => 'score_schema'),
      array('data' => '推荐意见', 'field' => 'opinion'),
      array('data' => '查看详情'),
      );
  $query = "SELECT guiding_fund_id FROM " .
           "{apply_file_info} where id = ?";
  $funds_list = db_query($query, array($apply_id));	
  $guiding_fund_id = '';
  foreach ($funds_list as $fund) {
      $guiding_fund_id = $fund->guiding_fund_id;
  }
  
  $query = db_select('expert_verdict', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'score_department',
      'score_applyman',
      'score_schema',
      'opinion',
      'expert_info_id',
      ))
	  ->condition('apply_id', $apply_id,'=')
	  ->condition('state', 1,'=')
	  ;
  
  $results = $query
      ->execute();

  $rows = array();
  $finish_expertlist = '';
  foreach ($results as $node) {
         $query = "SELECT id, name FROM " .
           "{expert_info} where userid = ? ";
		 $expert_list = db_query($query, array($node->expert_info_id));	
		 $current_expertid = '';
		 $current_expertname = '';
		 foreach ($expert_list as $expert) {
		      $current_expertid = $expert->id;
			  $current_expertname = $expert->name;
		  }	
		 $finish_expertlist .= $current_expertid .',';
		  $rows[] = array(
	      'data' => array(
	        l($current_expertname, 'expertmanage/list/'. $current_expertid),
	        $node->score_department,
	        $node->score_applyman,
	        $node->score_schema,
	        $node->opinion,
	        l('详细评审结果', 'expertmanage/graded_apply_view/'.  $node->id),
	      )
	      );
  }
  $query = "SELECT experts_list FROM " .
           "{guiding_fund_info} where id = ?";
  $experts_list = db_query($query, array($guiding_fund_id));	
  $experts = array();
  foreach ($experts_list as $expert) {
      $experts = explode(',',$expert->experts_list);
  }
  $finish_experts = explode(',',$finish_expertlist);
  foreach ($experts as $expert) {
  	if(!in_array($expert,$finish_experts, TRUE)){
  		$query = "SELECT id, name FROM " .
           "{expert_info} where id = ? ";
		$expert_list = db_query($query, array($expert));
		$current_expertid = '';
		$current_expertname = '';
		foreach ($expert_list as $expert) {
		      $current_expertid = $expert->id;
			  $current_expertname = $expert->name;
		}		
		if($current_expertid>0){
			$rows[] = array(
	      'data' => array(
	        l($current_expertname, 'expertmanage/list/'. $current_expertid),
	        '',
	        '',
	        '',
	        '',
	        '尚未提交评分结果',
	      )
	      );
		}
  		
  	}
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有专家评分结果',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}
 * 
 */

//对应基金所有申报材料平均专家评分结果列表
function expertmanage_allgraded_viewpage(){
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
  $html = '';	
  $page_content = '';
  $query = "SELECT funds_list,experts_list FROM " .
           "{pici} where state = ?";
  $queryResult = db_query($query, array(1));
  $funds_list = '';
  $experts_list = '';
  foreach($queryResult as $result){
  	$funds_list = $result->funds_list;
	$experts_list = $result->experts_list;
  }
  $experts = explode(',',$experts_list);
  $funds = explode(',',$funds_list);
  $page_content .= "<tr><td>拟申报基金名称</td>";
  $count_expert = 0;
  $count_fund = 0;
  $expert_name_table = '';//存专家名称横向表格
  $expert_name_array = array();//存专家姓名数组
  $fund_name_array = array();//存基金名称数组
  $opinion_array = array("推荐","有条件推荐","不推荐");
  foreach ($experts as $expert) {
  	   if($expert!=''){
  	   	 $count_expert +=1;
         $query = "SELECT name FROM " .
           "{expert_info} where id = ?";
  	     $experts_Name = db_query($query, array($expert));
	     foreach ($experts_Name as $expert_Name) {
	   		$expert_name_table .= "<td>".$expert_Name->name."</td>";
		    $expert_name_array[] = $expert_Name->name;
	     }
  	   }
  	  
  }
  //$count_expert -= 1;
  $page_content .= $expert_name_table. "<td >推荐票数</td>".
			  "<td>有条件推荐票数</td>".
			  "<td>不推荐票数</td></tr>";
  $expert_verdicts_assess = '';//综合意见table
  $expert_verdicts_opinion = '';//推荐意见table
  $expert_verdicts_remark = '';//备注table
  $expert_verdicts_score = '';//备注table
  
  foreach ($funds as $fundid) {
  	   if($fundid!=''){
  	   	$count_fund+=1;
	   $query = "SELECT nishejijinmingcheng FROM " .
           "{nishejijin} where id = ?";
  	   $funds_Name = db_query($query, array($fundid));
	   foreach ($funds_Name as $fund_Name) {
		    $fund_name_array[] = $fund_Name->nishejijinmingcheng;
		    $expert_verdicts_assess .= "<tr><td>".$fund_Name->nishejijinmingcheng."</td>";
  			$expert_verdicts_opinion .= "<tr><td>".$fund_Name->nishejijinmingcheng."</td>";
  			$expert_verdicts_remark .= "<tr><td>".$fund_Name->nishejijinmingcheng."</td>";
  			$expert_verdicts_score .= "<tr><td>".$fund_Name->nishejijinmingcheng."</td>";
	   }
	   $average_score = 0;
	   $count_tuijian = 0;
	   $count_tiaojiantuijian=0;
	   $count_butuijian = 0;
	   foreach ($experts as $expert) {
	          // $query = "SELECT id, assess,opinion,remark,score FROM " .
	           	// "{expert_verdict} where expert_info_id = ? and apply_id =? and state =?";
	           //$expert_verdicts = db_query($query, array($expert,$fundid,1));
	           $query = "SELECT id, assess,opinion,remark,score FROM " .
	           	 "{expert_verdict} where expert_info_id = ? and apply_id =? ";
	           $expert_verdicts = db_query($query, array($expert,$fundid));
			   $if_verdict = FALSE;
			   foreach ($expert_verdicts as $expert_verdict) {
			   		$if_verdict = TRUE;
					$expert_verdicts_assess .= "<td>".$expert_verdict->assess."</td>";
  					$expert_verdicts_opinion .= "<td>".$opinion_array[$expert_verdict->opinion]."</td>";
  					$expert_verdicts_remark .= "<td>".$expert_verdict->remark."</td>";
  					$expert_verdicts_score .= "<td>".$expert_verdict->score."</td>";
					$average_score += $expert_verdict->score;
					if($expert_verdict->opinion == 0)
						$count_tuijian +=1;
					else if($expert_verdict->opinion == 1)
						$count_tiaojiantuijian +=1;
					else {
						$count_butuijian += 1;
					}
			   }	
			   if(!$if_verdict&&$expert!='') {
			   	    $expert_verdicts_assess .= "<td></td>";
  					$expert_verdicts_opinion .= "<td></td>";
  					$expert_verdicts_remark .= "<td></td>";
  					$expert_verdicts_score .= "<td></td>";
			   }  	    
		}
        $average_score /= $count_expert;
		$expert_verdicts_opinion .= "<td>".$count_tuijian."</td>"."<td>".$count_tiaojiantuijian."</td>"."<td>".$count_butuijian."</td>";;
	    $expert_verdicts_score .= "<td>".$average_score."</td>";
	    $expert_verdicts_assess .= "</tr>";
  	    $expert_verdicts_opinion .= "</tr>";
  		$expert_verdicts_remark .= "</tr>";
  		$expert_verdicts_score .= "</tr>";
  	   }	   
  }
  $page_content .= $expert_verdicts_opinion;
  $page_content .=	"</table></div><br>";
  $colspan = $count_expert+4;
  $html .= "<div><table style='width: 850px'><tr><th colspan=".$colspan.">专家推荐情况汇总表</th></tr>";
  //$html .= "<div><table ><tr><th colspan=".$colspan.">专家推荐情况汇总表</th></tr>";
  $html	.= 	  $page_content;
  
  $page_content = '';
  $colspan = $count_expert +2;
  $html .= "<div><table style='width: 850px'><tr><th colspan=".$colspan.">专家分数汇总表</th></tr>";
  $page_content .= "<tr><td>拟申报基金名称</td>";
  $page_content .= $expert_name_table. "<td >平均分</td></tr>".$expert_verdicts_score."</table></div><br>" ;
  $html	.= 	  $page_content;
  
  $page_content = '';
  $colspan = $count_expert +1;
  $html .= "<div><table style='width: 850px'><tr><th colspan=".$colspan.">专家意见汇总</th></tr>";
  $page_content .= "<tr><td>拟申报基金名称</td>";
  $page_content .= $expert_name_table. "</tr>".$expert_verdicts_assess."</table></div><br>" ;
  $html	.= 	  $page_content;
  
  $page_content = '';
  $colspan = $count_expert +1;
  $html .= "<div><table style='width: 850px'><tr><th colspan=".$colspan.">专家备注汇总</th></tr>";
  $page_content .= "<tr><td>拟申报基金名称</td>";
  $page_content .= $expert_name_table. "</tr>".$expert_verdicts_remark."</table></div><br>" ;
  $html	.= 	  $page_content;
  $render_array = array(
	  '#markup' => $html,
  );
  $form = drupal_get_form('fund_pass_choose_form');
  $render_array['#markup'] .= drupal_render($form);
  return $render_array; 
}

function fund_pass_choose_form($form, &$form_state){
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
  $fund_list = get_part_by_state(3);
  $header = array(
    'nishejijinmingcheng' => t('拟申请基金名称'),
    'jijinguimo' => t('基金规模'),
    'zhuyaotouzilingyu' => t('主投领域'),
    'guanlijigoumingcheng' => t('机构名称'),
    'lianxiren' => t('联系人'),
  );  

	 
  $options = array();
  foreach ($fund_list as $fund) {
    $options[$fund['id']] =array(
      'nishejijinmingcheng' => $fund['nishejijinmingcheng'],
      'jijinguimo' => $fund['jijinguimo'],  
      'zhuyaotouzilingyu' => $fund['zhuyaotouzilingyu'],
      'guanlijigoumingcheng' => $fund['guanlijigoumingcheng'],
      'lianxiren' => $fund['lianxiren'],  
    );
  }

  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    
    '#options' => $options,
    '#empty' => t('没有基金记录'),
    
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('通过专家评审（若不勾选则表示专家评审未通过）'),
  );
  return $form;  
}

function fund_pass_choose_form_submit($form, &$form_state){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
	 $values = $form_state['values'];		
	 $funds_list = array();
	 foreach ($values['table'] as $rows) {
         $funds_list[] =$rows;
     }
	 $funds_array = get_part_by_state(3);
	 foreach ($funds_array as $fund) {
         if(in_array($fund['id'],$funds_list, TRUE)&&$funds_list!=NULL){
         	set_state_by_id(5,$fund['id']);
         }
		 else{
		 	set_state_by_id(4,$fund['id']);
		 }
     }
	 db_update('pici')
  		->fields(array('state' => 2))
  		->condition('state', 1)
  		->execute();
	$form_state['redirect'] = 'expertmanage/all_graded_view';
}

